package com.wen.moudle.Managers;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.wen.entry.Result;
import com.wen.entry.bean.DebrisCombineEquipment;
import com.wen.entry.bean.Good;
import com.wen.moudle.Account;
import com.wen.moudle.abstractManager;

import java.util.List;

public class BackpackManager extends abstractManager {
    public BackpackManager(Account account) {
        super(account);
    }


    // 获取物品
    public Result<List<Good>> goodsList(){
        HttpResponse response = HttpRequest.get(config.get("物品列表"))
                .cookie(getCookies()).execute();
        return JSONUtil.toBean(response.body(),
                new TypeReference<Result< List<Good> >>() {},
                false);
    }

    // 获取指定商品
    public Good getGood(String goodName){
        Result<List<Good>> resul = goodsList();
        for (Good datum : resul.getData()) {
            if (datum.getName().equals(goodName)){
                return datum;
            }
        }
        return null;
    }

    // 查询物品数量
    public Integer getGoodCount(String goodsName){
        Good good = getGood(goodsName);
        return good == null ? 0 : good.getCount();
    }

    // 获取物品ID
    public Integer getGoodId(String goodsName){
        Good good = getGood(goodsName);
        return good == null ? 0 : good.getId();
    }

    // 物品融合
    public Result<DebrisCombineEquipment> combine(String goodsName){
        HttpResponse response = HttpRequest.post(config.get("物品融合") + getGoodId(goodsName))
                .cookie(getCookies()).execute();
        return JSONUtil.toBean(response.body(),
                new TypeReference<Result<DebrisCombineEquipment>>() {},
                false);
    }

    // 物品融合
    public Result<DebrisCombineEquipment> combineGood(Integer goodsId){
        HttpResponse response = HttpRequest.post(config.get("物品融合")+goodsId)
                .cookie(getCookies()).execute();
        return JSONUtil.toBean(response.body(),
                new TypeReference<Result<DebrisCombineEquipment>>() {},
                false);
    }

    public Result<String> useGood(Integer goodsId){
        HttpResponse response = HttpRequest.post(config.get("使用物品")+goodsId)
                .cookie(getCookies()).execute();
        return JSONUtil.toBean(response.body(),
                new TypeReference<Result<String>>(){},
                false);
    }
}
